package 笔试.同行者实习;

import java.util.ArrayList;
import java.util.Arrays;

public class 字符串切割 {
    public static void main(String[] args) {
        String[] strings = mySplit("ab&&2");
        System.out.println(Arrays.toString(strings));
        System.out.println(myContact(strings));
    }

    public static String[] mySplit(String str) {
        int beg = 0;
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < str.length(); i++) {
            if (i + 1 < str.length() && str.charAt(i) == '&' && str.charAt(i + 1) == '&') {
                list.add(str.substring(beg, i));
                beg = i + 2;
                i++;
            }
        }
        list.add(str.substring(beg));
        return list.toArray(new String[0]);
    }

    public static String myContact(String[] strs) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < strs.length; i++) {
            sb.append(strs[i]);
            if (i != strs.length - 1) {
                sb.append("&&");
            }
        }
        return sb.toString();
    }
}